package com.gip.controller;

import com.gip.pojo.Employee;
import com.gip.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

//登录控制器
@Controller
public class LoginController {

    @Autowired
    EmployeeService employeeService;

    //跳转登录页面
    @RequestMapping(value = {"/", "/index"})
    public String login() {
        return "index";
    }

    //执行登录
    @PostMapping("/toLogin")
    public String toLogin(Employee employee, HttpSession session) {
        Employee employeeById = employeeService.findEmployeeById(employee);
        if (employeeById != null) {
            session.setAttribute("Employee", employeeById);
            return "main";
        }
        session.setAttribute("msg", "密码错误");
        return "index";
    }

    //注销
    @RequestMapping("/layout")
    public String layout(HttpSession session) {
        session.invalidate();
        return "index";
    }
}
